import { pinyin } from 'pinyin-pro';

export function toPage (url, params = {}) {
  this.$u.route({
    url: url,
    params
  })
}

export function textToPy (text) {
  return pinyin(text, { toneType: 'none' }).replace(' ', '')
}


export function pinyinSort (arr) {
  arr.sort((a, b)=>{
    // return b.name - a.name
    return a.pname.charCodeAt(0) - b.pname.charCodeAt(0)
  });

  let tempArr = []
  arr.forEach(item=>{
    const firstLetter = item.pname.charAt(0).toUpperCase()
    console.log('firstLetter', firstLetter, !tempArr[firstLetter], item)
    if (!tempArr[firstLetter]) {
      tempArr[firstLetter] = [];
    }
    tempArr[firstLetter].push(item);
  });
  return tempArr;
}

export function groupBy (arr, key) {
  return arr.reduce((result, item)=>{
    const groupKey = item[key];
    if (!result[groupKey]) {
      result[groupKey] = [];
    }
    result[groupKey].push(item);
    return result;
  }, {});
}

// dayjs格式化时间
export function formatDate (date, fmt = 'YYYY-MM-DD HH:mm:ss') {

  if (!date) {
    return ''
  }
  return dayjs(date).format(fmt);
}